package org.hyl.javabase.多线程;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;

public class process {

	static String userdir = System.getProperty("user.dir");
	static String cmd = userdir + "/src/javabase/test.bat";// 指定文件

	public void execbat0() {

		try {
			Runtime rt = Runtime.getRuntime();
			String cmdstr="cmd /c dir";
			Process proc = rt.exec(cmdstr);
			InputStream stdin = proc.getInputStream();
			InputStreamReader isr = new InputStreamReader(stdin);
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			while ((line = br.readLine()) != null)
				System.out.println(line);
			System.out.println("");
			int exitVal = proc.waitFor();
			System.out.println("Process exitValue: " + exitVal);
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}

	public void execjava0() {
		// 设置环境变量
		 String[] env = { "JAVA_HOME=C:/Program Files/Java/jdk1.8.0_66",
		 "PATH=C:/Program Files/Java/jdk1.8.0_66/bin" };
		// 设置工作目录
		 File dir = new File("D:/");
		try {
			Runtime rt = Runtime.getRuntime();
			// Process proc = rt.exec(new String[] { "cmd", "/c", "java",
			// "-version" });
			// Process ps = rt.exec("cmd /c java -version");
			Process ps = rt.exec(new String[] { "cmd", "/c", "java", "-version" },env,dir);
			Scanner scanner1 = new Scanner(ps.getInputStream());
			Scanner scanner2 = new Scanner(ps.getErrorStream());
			StringBuilder result = new StringBuilder();
			while (scanner1.hasNextLine()) {
				result.append(scanner1.nextLine());
				result.append(System.getProperty("line.separator"));
			}
			while (scanner2.hasNextLine()) {
				result.append(scanner2.nextLine());
				result.append(System.getProperty("line.separator"));
			}
			scanner1.close();
			scanner2.close();
			System.out.println(result.toString());
			;
			int exitVal = ps.waitFor();
			System.out.println("Process exitValue: " + exitVal);
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}

	public void execjava1() throws InterruptedException, IOException {
		ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "java", "-version");
		// 如果将值设置为 true，标准错误将与标准输出合并。这使得关联错误消息和相应的输出变得更容易。
		// 在此情况下，合并的数据可从 Process.getInputStream() 返回的流读取
		pb.redirectErrorStream(true);
		pb.directory(new File("D:/"));
	//	String path = pb.environment().get("Path");
		pb.environment().put("Path", "C:/Program Files/Java/jdk1.8.0_66/bin");
		pb.environment().put("JAVA_HOME", "C:/Program Files/Java/jdk1.8.0_66");

		Process ps = pb.start();
		Scanner scanner = new Scanner(ps.getInputStream());
		StringBuilder result = new StringBuilder();
		while (scanner.hasNextLine()) {
			result.append(scanner.nextLine());
			result.append(System.getProperty("line.separator"));
		}

		scanner.close();
		System.out.println(result.toString());

	}

	public void execbat1() {

		// process = Runtime.getRuntime().exec(mContext.getApplicationContext().
		// getApplicationContext().getFilesDir().getAbsolutePath() + "/" +
		// copyFileNameList.get(finalI) + " " + copyFileExArgs.get(finalI));
		Process p;
		try {
			// 执行命令
			ProcessBuilder pb = new ProcessBuilder("command", "param1", "param2");
			// Map<String, String> env = pb.environment();
			// env.put("VAR", "Value");
			p = pb.start();
			// 取得命令结果的输出流
			InputStream fis = p.getInputStream();
			// 用一个读输出流类去读
			InputStreamReader isr = new InputStreamReader(fis);
			// 用缓冲器读行
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			// 直到读完为止
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
